# Copyright:: Copyright (c) 2008 Howling Moon Software



class PreMultAlphaBlendNode < AS::Scene::BlendNode
	def initialize(child)
		super(nil, GL::ONE, GL::ONE_MINUS_SRC_ALPHA, child)
	end
end

class FancyTextNode < AS::Scene::ColorNode

  attr_reader :x, :y, :width, :height

	def initialize(xoffset, yoffset, font_scale, color, text, centered = true, font = AS::Font::DefaultFont)
	
    @width = font.string_width(text)*font_scale
    @height = font.height*font_scale

 		@x = xoffset
		@y = yoffset
 		@x -= @width*0.5 if centered
 		@y -= @height*0.5 if centered
 		
		packed_color = AS::PackedColor.new(color)
		child = PreMultAlphaBlendNode.new{
			matrix = AS::Transform2D.translate(@x, @y)
			matrix *= AS::Transform2D.scale(font_scale, font_scale)
			packed_matrix = AS::PackedMatrix.new(matrix)
			AS::Scene::MatrixNode.new(GL::MODELVIEW, packed_matrix){
				AS::Scene::TextNode.new(font, text)
			}
		}

		super(packed_color, child)
	end	
end
 
class EventDemo

	def initialize
		super
		
 		@text = "Warming up..."
 		@fire = false
 		 
		@input_pad = AS::Event::InputPad.new
		@input_pad.kaxis(:x, :Key_LEFT, :Key_RIGHT)
		@input_pad.kaxis(:y, :Key_DOWN, :Key_UP)
		@input_pad.key(:fire, :Key_SPACE){|pressed| @fire = true if pressed}

		@view_root = AS::Scene::GroupNode.new [
			FancyTextNode.new(20,HEIGHT-70,2, AS::Color::Red, "Use the arrow keys and tap the space bar to try axis and key events.", false),
			FancyTextNode.new(50,HEIGHT/2,3.0, AS::Color::Green, @text, false),
		]	
	end
	
	def input(event)
		@input_pad.call(event)
	end
	
	def update
		@text.replace("Axis: " + [@input_pad[:x], @input_pad[:y]].inspect)
		@text.replace(@text +" and FIRE!!!") if @fire
	
		@fire = false
	end
	
	def draw
		AS.clear_screen
		@view_root.render
		#AS.print_gl_errors - We took this out of aerosol, as part of some refactoring.
	end
end
